<?php

/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
    class Verify extends MX_Controller{
        
        public function __construct() {
            parent::__construct();
            $this->load->helper(array("url","form"));
            $this->load->library(array("form_validation","session","my_auth","my_layout"));
            $this->load->model('user_model');
            
            
        }
        public function login()
        {
            if($this->my_auth->is_Admin())
            {
                redirect(site_url()."/admin/admin/home");
                exit();
            }
            $this->form_validation->set_rules("username","Username","required");
            $this->form_validation->set_rules("password","Password","required");
            
            if($this->form_validation->run() == FALSE)
            {
                $this->load->view("login", array("error"=>""));
            }
            else
            {
                $u = $this->input->post('username');
                $p = $this->input->post('password');
                $session = $this->user_model->checkLogin($u, $p);
                if($session)
                {
                    $data = array(
                                    "username"  => $session['username'],
                                    "id_user"    => $session['id_user'],
                                    "level"  => $session['level'],
                                );
                    $this->my_auth->set_userdata($data);
                    if($data['level'] == 1){
                        redirect(site_url()."/admin/admin/home");
                    }
                    else if($data['level'] == 2)
                    {
                        redirect(site_url()."/admin/manager/home");
                    }
                    
                }
                else
                {
                    $this->load->view("login", array("error"=>"User or password wrong"));
                }
            }
        }
        //end login
        //---- Logout
        function logout()
        {
            $this->my_auth->sess_destroy();
            redirect(site_url()."/admin/verify/login");
        }
    }
?>

